/*********************************************************************
 * INCLUDES
 */

#include "bcomdef.h"
#include "OSAL.h"
#include "OSAL_PwrMgr.h"

#include "OnBoard.h"
#include "hal_adc.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
/*
#include "gatt.h"

#include "hci.h"

#include "gapgattserver.h"
#include "gattservapp.h"
#include "pulseGATTprofile.h"*/


#include "pulsePeripheral.h"
#include "itoa.h"
#include "qrsdet2.h"
#include "samples.h"


/*********************************************************************
 * MACROS
 */

/*********************************************************************
 * CONSTANTS
 */

// How often to perform periodic event
#define PULSE_EVT_PERIOD                  2

/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */

/*********************************************************************
 * EXTERNAL VARIABLES
 */

/*********************************************************************
 * EXTERNAL FUNCTIONS
 */

/*********************************************************************
 * LOCAL VARIABLES
 */
static uint8 pulseBLEPeripheral_TaskID;   // Task ID for internal task/event processing
static uint8 buf[32+1];

/*********************************************************************
 * LOCAL FUNCTIONS
 */
static void pulseBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg );
static void performPeriodicTask( void );




/*********************************************************************
 * PUBLIC FUNCTIONS
 */

/*********************************************************************
 * @fn      PulseBLEPeripheral_Init
 *
 * @brief   Initialization function for the Pulse BLE Peripheral App Task.
 *          This is called during initialization and should contain
 *          any application specific initialization (ie. hardware
 *          initialization/setup, table initialization, power up
 *          notificaiton ... ).
 *
 * @param   task_id - the ID assigned by OSAL.  This ID should be
 *                    used to send messages and set timers.
 *
 * @return  none
 */
void PulseBLEPeripheral_Init( uint8 task_id )
{
  pulseBLEPeripheral_TaskID = task_id;

  P1SEL = 0x30;
  P1 = 0;
  PERCFG |= 0x01;
  P1DIR = 0xFF; //0xEF

  HalUARTInit();
 
  halUARTCfg_t config;
  
  config.configured = true;
  
  config.baudRate = HAL_UART_BR_115200;
  
  config.flowControl = false;
  
  config.flowControlThreshold = 64;
  
  config.idleTimeout = 6;
  
  config.rx.maxBufSize = 128;
  
  config.tx.maxBufSize = 128;
  
  config.intEnable = false;
  
  config.callBackFunc = NULL;
  HalUARTOpen(HAL_UART_PORT_0, &config);
  
  
  
  // Setup a delayed profile startup
  osal_set_event( pulseBLEPeripheral_TaskID, START_DEVICE_EVT );
}

/*********************************************************************
 * @fn      PulseBLEPeripheral_ProcessEvent
 *
 * @brief   Pulse BLE Peripheral Application Task event processor.  This function
 *          is called to process all events for the task.  Events
 *          include timers, messages and any other user defined events.
 *
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process.  This is a bit map and can
 *                   contain more than one event.
 *
 * @return  events not processed
 */
uint16 PulseBLEPeripheral_ProcessEvent( uint8 task_id, uint16 events )
{
  
  VOID task_id; // OSAL required parameter that isn't used in this function
  
  if ( events & SYS_EVENT_MSG )
  {
    uint8 *pMsg;

    if ( (pMsg = osal_msg_receive( pulseBLEPeripheral_TaskID )) != NULL )
    {
      pulseBLEPeripheral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg );

      // Release the OSAL message
      VOID osal_msg_deallocate( pMsg );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }

  if ( events & START_DEVICE_EVT )
  {
    // Set timer for first periodic event
    osal_start_timerEx( pulseBLEPeripheral_TaskID, PERIODIC_EVT, PULSE_EVT_PERIOD );    
    
    // Initialize QRS detection
    QRSDet(0,1) ;
     
    return ( events ^ START_DEVICE_EVT );
  }

  if ( events & PERIODIC_EVT )
  {
    // Restart timer
    if (PERIODIC_EVT)
      osal_start_timerEx( pulseBLEPeripheral_TaskID, PERIODIC_EVT, PULSE_EVT_PERIOD );
  
    // Perform periodic application task
    performPeriodicTask();
    
    return (events ^ PERIODIC_EVT);
  }  
     
  
  // Discard unknown events
  return 0;
}

/*********************************************************************
 * @fn      pulseBLEPeripheral_ProcessOSALMsg
 *
 * @brief   Process an incoming task message.
 *
 * @param   pMsg - message to process
 *
 * @return  none
 */
static void pulseBLEPeripheral_ProcessOSALMsg( osal_event_hdr_t *pMsg )
{
  switch ( pMsg->event )
  {
    case KEY_CHANGE:

      break;
  }
}



/*********************************************************************
 * @fn      performPeriodicTask
 *
 * @brief   Perform a periodic application task. This function gets
 *          called every five seconds as a result of the PERIODIC_EVT
 *          OSAL event. In this example, the value of the third 
 *          characteristic in the PulseGATTProfile service is retrieved
 *          from the profile, and then copied into the value of the
 *          the fourth characteristic.
 *
 * @param   none
 *
 * @return  none
 */
static void performPeriodicTask( void )
{
 int adc_read, ret, i = 0;
 
 adc_read = HalAdcRead( 0, HAL_ADC_RESOLUTION_12 ); 
 adc_read <<=4;
 
 ret = QRSDet(adc_read, 0);

//ret = QRSDet(samples[i++], 0);
 
}







/*********************************************************************
*********************************************************************/